//
//  YLPlayingViewController.m
//  音乐播放器
//
//  Created by 袁量 on 2017/6/5.
//  Copyright © 2017年 袁量. All rights reserved.
//

#import "YLPlayingViewController.h"
#import "YLMusic.h"
#import "YLMusicTool.h"
#import "NSString+XMGTimeExtension.h"
#import "CALayer+PauseAimate.h"
#import "YLAudioTool.h"
#import "YLLrcView.h"
#import "YLLrcView.h"
#import "YLLrcCell.h"
#import "YLLrcLabel.h"
#import "YLMusicView.h"

@interface YLPlayingViewController ()<UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *albumView;
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *songLabel;
@property (weak, nonatomic) IBOutlet UILabel *singerLabel;
@property (weak, nonatomic) IBOutlet UILabel *currentTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalTimeLabel;
/** 进度条时间 */
@property (nonatomic, strong) NSTimer *progressTimer;
/** 歌词的定时器 */
@property (nonatomic, strong) CADisplayLink *lrcTimer;
@property (weak, nonatomic) IBOutlet UIButton *playOrPauseBtn;
@property (weak, nonatomic) IBOutlet YLLrcView *lrcView;
@property (weak, nonatomic) IBOutlet YLLrcLabel *lrcLabel;

/** 播放器 */
@property (nonatomic, strong) AVAudioPlayer *currentPlayer;

- (IBAction)start;
- (IBAction)end;
- (IBAction)progressValueChange;
- (IBAction)sliderClick:(UITapGestureRecognizer *)sender;
- (IBAction)playOrPause;
- (IBAction)next:(id)sender;
- (IBAction)previous:(id)sender;
- (IBAction)pushMusicTableView;




@end

@implementation YLPlayingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.lrcView.lrcLabel = self.lrcLabel;
    [self setUpUI];
//    self.lrcView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setUpUI {
    [self.progressSlider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    [self startPlayingMusick];
}
- (void)startPlayingMusick {
    YLMusic *playingMusic = [YLMusicTool playingMusic];
    _currentPlayer = [YLAudioTool playMusicWithFileName:playingMusic.filename];
    self.albumView.image = [UIImage imageNamed:playingMusic.icon];
    self.iconView.image = [UIImage imageNamed:playingMusic.icon];
    self.songLabel.text = playingMusic.name;
    self.singerLabel.text = playingMusic.singer;
    self.currentTimeLabel.text = [NSString stringWithTime:_currentPlayer.currentTime];
    self.totalTimeLabel.text = [NSString stringWithTime:_currentPlayer.duration];
    
    
    self.playOrPauseBtn.selected = self.currentPlayer.isPlaying;
//    设置歌词
    self.lrcView.lrcName = playingMusic.lrcname;
    [self removeProgressTimer];
    [self addProgressTimer];
    [self removeLrcTimer];
    [self addLrcTimer];
    
    
    [self addIconViewAnimate];
    
}
#pragma mark - 添加iconView的动画 
- (void)addIconViewAnimate
{
    CABasicAnimation *rotateAnimate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotateAnimate.fromValue = @(0);
    rotateAnimate.toValue = @(M_PI * 2);
    rotateAnimate.repeatCount = NSIntegerMax;
    rotateAnimate.duration = 35;
    [self.iconView.layer addAnimation:rotateAnimate forKey:nil];
}
- (void)viewWillLayoutSubviews{
    self.iconView.layer.cornerRadius = self.iconView.bounds.size.width * 0.5;
    self.iconView.layer.masksToBounds = YES;
    self.iconView.layer.borderColor = [UIColor colorWithRed:36 / 255.0 green:36 / 255.0 blue:36 / 255.0 alpha:1.0].CGColor;
    self.iconView.layer.borderWidth = 8;
}
#pragma mark - 对进度条时间的处理
- (void)addProgressTimer
{
    // 1.提前更新数据
    [self updateProgressInfo];
    
    // 2.添加定时器
    self.progressTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateProgressInfo) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.progressTimer  forMode:NSRunLoopCommonModes];
}

#pragma mark 移除定时器
- (void)removeProgressTimer
{
    [self.progressTimer invalidate];
    self.progressTimer = nil;
}
#pragma mark - 更新进度条
- (void)updateProgressInfo
{
    // 1.更新播放的时间
    self.currentTimeLabel.text = [NSString stringWithTime:self.currentPlayer.currentTime];
    
    // 2.更新滑动条
    self.progressSlider.value = self.currentPlayer.currentTime / self.currentPlayer.duration;
}
- (IBAction)start {
    [self removeProgressTimer];
}

- (IBAction)end {
    self.currentPlayer.currentTime = self.progressSlider.value * self.currentPlayer.duration;
    [self addProgressTimer];
}

- (IBAction)progressValueChange {
    self.currentTimeLabel.text = [NSString stringWithTime:self.progressSlider.value * self.currentPlayer. duration];
}

- (IBAction)sliderClick:(UITapGestureRecognizer *)sender {
    // 1.获取点击到的点
    CGPoint point = [sender locationInView:sender.view];
    
    // 2.获取点击的比例
    CGFloat ratio = point.x / self.progressSlider.bounds.size.width;
    
    // 3.更新播放的时间
    self.currentPlayer.currentTime = self.currentPlayer.duration * ratio;
    
    // 4.更新时间和滑块的位置
    [self updateProgressInfo];
}

- (IBAction)playOrPause {
    self.playOrPauseBtn.selected = !self.playOrPauseBtn.selected;
    if (self.currentPlayer.playing) {
        // 1.暂停播放器
        [self.currentPlayer pause];
        
        // 2.移除定时器
        [self removeProgressTimer];
        
        // 3.暂停旋转动画
        [self.iconView.layer pauseAnimate];
        
    } else {
        // 1.开始播放
        [self.currentPlayer play];
        
        // 2.添加定时器
        [self addProgressTimer];
        
        // 3.恢复动画
        [self.iconView.layer resumeAnimate];
    }

}

- (IBAction)next:(id)sender {
    // 1.获取下一首歌
    YLMusic *nextMusic = [YLMusicTool nextMusic];
    
    // 2.播放下一首音乐
    [self playMusicWithMusic:nextMusic];
}

- (IBAction)previous:(id)sender {
    // 1.取出上一首音乐
    YLMusic *previousMusic = [YLMusicTool previousMusic];
    
    // 2.播放上一首音乐
    [self playMusicWithMusic:previousMusic];
}

- (IBAction)pushMusicTableView {
//    YLMusicView *view = [YLMusicView new];
//    view.frame = CGRectMake(0, 100, 414, 500);
//    [self.view addSubview:view];
}
- (void)playMusicWithMusic:(YLMusic *)muisc
{
    // 1.获取当前播放的歌曲并停止
    YLMusic *currentMusic = [YLMusicTool playingMusic];
    [YLAudioTool stopMusicWithFileName:currentMusic.filename];
    
    // 2.设置上一首歌为默认播放的歌曲
    [YLMusicTool setupPlayingMusic:muisc];
    
    // 3.播放音乐,并更新界面信息
    [self startPlayingMusick];
}
#pragma mark UIScrollView 代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 1.获取滑动的偏移量
    CGPoint point =  scrollView.contentOffset;
    
    // 2.获取滑动比例
    CGFloat alpha = 1 - point.x / scrollView.bounds.size.width;
    
    // 3.设置alpha
    self.iconView.alpha = alpha;
    self.lrcLabel.alpha = alpha;
}
#pragma mark - 对歌词定时器的处理
- (void)addLrcTimer
{
    self.lrcTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateLrcInfo)];
    [self.lrcTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)removeLrcTimer
{
    [self.lrcTimer invalidate];
    self.lrcTimer = nil;
}

#pragma mark 更新歌词进度
- (void)updateLrcInfo
{
    self.lrcView.currentTime = self.currentPlayer.currentTime;
}

#pragma mark - 改变状态栏的文字颜色
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
@end
